package com.it.controller;

import com.it.entity.User;
import com.it.entity.WalletFollow;
import com.it.service.UserService;
import com.it.service.WalletFollowService;
import com.it.vto.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xuChangSong
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO
 * @createTime 2022年11月09日 15:03:00
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private WalletFollowService walletFollowService;



    @RequestMapping(value = "/getUserBalanceInfo")
    public User getUser(String userId){
        User userInfo = userService.getUserInfoById(userId);
        return userInfo;
    }

    @RequestMapping(value = "/userConsumption")
    public CommonResult userConsumption(String userId,String payMoney){
        /*这里的价格应该从商品表里面获取出来，暂时不做那一部分，也不对payMoney字符串做校验了*/
        double payMoneyValue = Double.parseDouble(payMoney);
        User userInfo = userService.getUserInfoById(userId);
        if(userInfo != null && userInfo.getBalance() >= payMoneyValue){
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("id", userId);
            paramMap.put("payMoney", payMoneyValue);
            int count = userService.userConsumption(paramMap);

            WalletFollow walletFollow = new WalletFollow();
            walletFollow.setCreateDate(new Date());
            walletFollow.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
            walletFollow.setMoney(payMoneyValue);
            walletFollow.setUser(userInfo);
            //这里的推付款应该作为枚举，暂时不处理了！！！
            walletFollow.setDescribe("付款");
            walletFollowService.insertWalletFollow(walletFollow);

            return new CommonResult(200,"支付成功！" );
        }else{
            return new CommonResult(200,"当前用户余额不足，请先充值或采用其他方式支付" );
        }
    }

    @RequestMapping(value = "/userRefund")
    public CommonResult userRefund(String userId,String payMoney){
        /*这里的价格应该从商品表里面获取出来，暂时不做那一部分，也不对payMoney字符串做校验了*/
        double payMoneyValue = Double.parseDouble(payMoney);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id", userId);
        paramMap.put("refund", payMoneyValue);
        int count = userService.userRefund(paramMap);
        User userInfo = new User();
        userInfo.setId(userId);
        WalletFollow walletFollow = new WalletFollow();
        walletFollow.setCreateDate(new Date());
        walletFollow.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
        walletFollow.setMoney(payMoneyValue);
        walletFollow.setUser(userInfo);
        //这里的推付款应该作为枚举，暂时不处理了！！！
        walletFollow.setDescribe("退款");
        walletFollowService.insertWalletFollow(walletFollow);
        return new CommonResult(200,"退款成功！" );
    }

    @RequestMapping(value = "/getUserdBalanceDetailed")
    public CommonResult getUserdBalanceDetailed(String userId){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("userId", userId);
        List<WalletFollow> walletFollows = walletFollowService.getwalletfollowInfo(paramMap);
        return new CommonResult(200,"退款成功！" ,walletFollows);
    }



}
